﻿using System;
using System.Reflection;
using Microsoft.SharePoint;

namespace BoldBrick.SharePoint.CommonControls.DataObjects.Base
{
	/// <summary>
	/// Metadata class for event receiver object
	/// </summary>
	public abstract class BaseEventReceiverObject
	{
		#region Properties
		/// <summary>
		/// Gets <see cref="SPEventReceiverType"/> type of the event receiver object
		/// </summary>
		public SPEventReceiverType EventReceiverType
		{
			get
			{
				return GetEventReceiverType();
			}
		}

		private Type type;
		/// <summary>
		/// Gets full name of type of event receiver object
		/// </summary>
		public string EventReceiverClassFullName
		{
			get
			{
				if ( type == null )
					type = GetEventReceiverClassType();
				
				return type.FullName;
			}
		}

		private Assembly assembly;
		/// <summary>
		/// Gets full name of event receiver object assembly
		/// </summary>
		public virtual string AssemblyFullName
		{
			get
			{
				if ( assembly == null )
					assembly = GetType().Assembly;

				return assembly.FullName;
			}
		}
		
		/// <summary>
		/// Gets sequence number of event receiver object
		/// </summary>
		public int SequenceNumber
		{
			get
			{
				return GetSequenceNumber();
			}
		}
		

		#endregion

		
		#region Abstract & Virtual Methods

		/// <summary>
		/// Gets <see cref="SPEventReceiverType"/> type of the event receiver object
		/// </summary>
		/// <returns>Returns <see cref="SPEventReceiverType"/> type of the event receiver object</returns>
		protected abstract SPEventReceiverType GetEventReceiverType();

		/// <summary>
		/// Gets type of event receiver object
		/// </summary>
		/// <returns>Returns type of event receiver object</returns>
		protected abstract Type GetEventReceiverClassType();

		/// <summary>
		/// Gets sequence number of event receiver object
		/// </summary>
		/// <returns>Returns sequence number of event receiver object</returns>
		protected virtual int GetSequenceNumber()
		{
			return 10000;
		}

		#endregion


		#region Methods
		
		/// <summary>
		/// Method for comparation with <see cref="SPEventReceiverDefinition"/> object
		/// </summary>
		/// <param name="eventReceiverDefinition"><see cref="SPEventReceiverDefinition"/> object to compare</param>
		/// <returns>Returns true if objects are equal, otherwise returns false</returns>
		public bool IsEqualToDefinition(SPEventReceiverDefinition eventReceiverDefinition)
		{
			bool equal =
				eventReceiverDefinition.Assembly == this.AssemblyFullName &&
				eventReceiverDefinition.Class == this.EventReceiverClassFullName &&
				eventReceiverDefinition.Type == this.EventReceiverType;
			return equal;
		}

		public override string ToString()
		{
			return string.Format("{0}, {1}, {2}", this.EventReceiverType, this.EventReceiverClassFullName, this.AssemblyFullName);
		}

		#endregion

	}
}
